package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;





public class MyFrame extends JFrame implements ActionListener{

	private static final String ACTION_LOAD_FIBER = "ACTION_LOAD_FIBER";
	private static final String ACTION_LOAD_VOLUME = "ACTION_LOAD_VOLUME";
	private AsuvizPanelRenderer panel;
	private JButton buttonLoadFiber;
	private JButton buttonLoadVolume;
	private JPanel panelVolumeValues;
	public MyFrame()
	{
		setLayout(new BorderLayout());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(600,600);
		
		panel = new AsuvizPanelRenderer();
		panel.setStatsRelativePath("//..//..//..//Resultados//stats//aseg.stats");
		buttonLoadFiber = new JButton("Load Fiber");
		buttonLoadFiber.addActionListener(this);
		buttonLoadFiber.setActionCommand(ACTION_LOAD_FIBER);
		buttonLoadVolume = new JButton("Load Volume");
		buttonLoadVolume.addActionListener(this);
		buttonLoadVolume.setActionCommand(ACTION_LOAD_VOLUME);
		
		panelVolumeValues = new JPanel();
		panelVolumeValues.setLayout(new BorderLayout());
		panelVolumeValues.setBorder(BorderFactory.createTitledBorder("Info"));
		
		add(panel, BorderLayout.CENTER);
		JPanel myPanel = new JPanel(new GridLayout(2, 1));
		myPanel.add(buttonLoadFiber);
		myPanel.add(buttonLoadVolume);
		add(myPanel, BorderLayout.SOUTH);
		add(panelVolumeValues, BorderLayout.EAST);
		this.setVisible(true);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new MyFrame();
	}
	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		String action = actionEvent.getActionCommand();
		if(action.equals(ACTION_LOAD_FIBER))
		{
			try {
				File[] files = getFile();
				if(files != null)
				{
					for(File f : files)
					{
						Color c = getColor(f.getAbsolutePath());
						panel.addFiber(f, c);
					}
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		else if(action.equals(ACTION_LOAD_VOLUME))
		{
			try {
				File[] files = getFile();
				if(files != null)
				{
					for(File file : files)
					{
						Color c = getColor(file.getAbsolutePath());
						panel.addVolume(file, c);
						updatePanelVolumeValues();
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	private void updatePanelVolumeValues() {
		panelVolumeValues.removeAll();
		String[] volumeNames = panel.getVolumeNames();
		Color[] volumeColors = panel.getVolumeColors();
		JPanel panelVolumeValuesTemp = new JPanel(new GridLayout(volumeNames.length, 1));
		for(int i = 0; i < volumeNames.length; i++)
		{
			JPanel panelLabels = new JPanel(new BorderLayout());
			panelLabels.add(new JLabel(volumeNames[i]), BorderLayout.CENTER);
			JLabel labelVolumeColorI = new JLabel();
			labelVolumeColorI.setOpaque(true);
			labelVolumeColorI.setBackground(volumeColors[i]);
			labelVolumeColorI.setPreferredSize(new Dimension(25, 0));
			panelLabels.add(labelVolumeColorI, BorderLayout.EAST);
			panelVolumeValuesTemp.add(panelLabels);
		}
		panelVolumeValues.add(panelVolumeValuesTemp, BorderLayout.NORTH);
		panelVolumeValues.revalidate();
	}
	private File[] getFile()
	{
		File[] file = null;
		JFileChooser fileChooser = new JFileChooser("File");
		fileChooser.setFileFilter(new FileNameExtensionFilter("VTK Files", new String[]{"vtk"}));
		fileChooser.setMultiSelectionEnabled(true);
		int option = fileChooser.showOpenDialog(this);
		if(option == JFileChooser.APPROVE_OPTION)
		{
			file = fileChooser.getSelectedFiles();
		}
		return file;
	}
	
	private Color getColor(String structureName)
	{
		structureName = structureName.substring(structureName.lastIndexOf(File.separatorChar) + 1);
		Color color = JColorChooser.showDialog(this, "Select color for " + structureName, Color.LIGHT_GRAY);
		return color;
	}

}
